package design.behavior_chain;

import org.junit.Test;

/**
 * 责任链
 */
public class DemoResponsibilityChain {
    @Test
    public void test(){
        Approver zhangsan = new Employee("张三");
        zhangsan.setNextApprover(new Manager("李四")).setNextApprover(new CEO("王五"));
        zhangsan.approve(10000);
    }
    public abstract class Approver{
        protected String name;
        protected Approver nextApprover;

        public Approver(String name) {
            this.name = name;
        }
        protected Approver setNextApprover(Approver nextApprover) {
            this.nextApprover = nextApprover;
            return this.nextApprover;
        }
        public abstract void approve(int amount);
    }
    public class Employee  extends Approver {
        public Employee(String name) {
            super(name);
        }
        @Override
        public void approve(int amount) {
            if (amount <= 1000) {
                System.out.println("审批通过。【员工：" + name + "】");
            }else{
                System.out.println("无权审批，升级处理。【员工：" + name + "】");
                this.nextApprover.approve(amount);
            }
        }
    }

    public class Manager  extends Approver {
        public Manager (String name) {
            super(name);
        }
        @Override
        public void approve(int amount) {
            if (amount <= 5000) {
                System.out.println("审批通过。【经理：" + name + "】");
            }else{
                System.out.println("无权审批，升级处理。【经理：" + name + "】");
                this.nextApprover.approve(amount);
            }
        }
    }

    public class CEO  extends Approver {
        public CEO (String name) {
            super(name);
        }
        @Override
        public void approve(int amount) {
            if (amount <= 10000) {
                System.out.println("审批通过。【CEO：" + name + "】");
            }else{
                System.out.println("驳回申请。【CEO：" + name + "】");
                this.nextApprover.approve(amount);
            }
        }
    }
}
